/*
 * @页面名称: 对更改的业务数据进行字段规则校验(克隆专用)
 * @描述:
 * @作者: 研发中心
 * @Date: 2023-07-18 09:00:05
 */
import { removeBlock } from "./until";
import { useDynamicStore } from "@/stores/modules/dynamic";
import { useCatalogStore } from "@/stores/modules/catalog";
import dtime from "moment";
import vueAxios from "@/api/index";

export class TestClass {
  data: any; //业务数据
  filedList: any[]; //字段规则
  tableName?: string; //表名
  dynamicStore: any;
  catalogStore: any;
  constructor(data: any, filedList: any[], tableName?: string) {
    this.data = data;
    this.filedList = filedList;
    this.tableName = tableName;
    this.dynamicStore = useDynamicStore();
    this.catalogStore = useCatalogStore();
  }
  async checkout() {
    const { data, filedList, tableName } = this;
    const { fatherObj, tableSelect } = this.dynamicStore;
    for (let i = 0; i < filedList.length; i++) {
      const item = filedList[i];
      if (item.FR_RuleType === "init") {
        console.log("初始化");
        let str = removeBlock(item.FR_Rule.trim());
        if (data[item.FR_TableField]) {
          // 有值的时候就不触发(比如编辑的时候)
        } else if (str.startsWith(".")) {
          // 解析以.开头的字符串
          let FieldStr = str.slice(1);
          if (data.Parent_ID == null || data.Parent_ID == "") {
            // return;
          } else {
            data[item.FR_TableField] = fatherObj && fatherObj[FieldStr] ? fatherObj && fatherObj[FieldStr] : "";
          }
        } else if (str.includes("LSH:")) {
          // 获取流水号
          const Res: any = await vueAxios.post(
            "Dynamic/FunctionGetMaxByCondition",
            { tableName: this.tableName, fieldRule: item.FR_Rule, data: data },
            { noLoading: true }
          );
          if (Res.Mark) {
            data[item.FR_TableField] = Res.Value;
          }
        } else if (str.includes("dateTime:")) {
          const param = str.split(":");
          const newStr = [] as any[];
          param.forEach((item, index) => {
            if (index > 0) {
              newStr.push(item);
            }
          });
          console.log("````````````init触发时间函数", newStr.join(":"));
          data[item.FR_TableField] = dtime(new Date()).format(newStr.join(":"));
        } else if (str.includes("currCatalog.")) {
          console.log("string解析", str);
          const Code = this.catalogStore.treeNode.Catalog_Code + "·" + data.Year + "-" + this.PadLeft(data.Order);
          // eslint-disable-next-line no-eval
          data[item.FR_TableField] = Code;
        } else if (str.includes("currOrg.")) {
          // eslint-disable-next-line no-eval
          data[item.FR_TableField] = eval(str);
        } else if (str.includes("currUser.")) {
          // eslint-disable-next-line no-eval
          data[item.Field_Name] = eval(str);
        } else {
          console.log(item);

          try {
            data[item.FR_TableField] = eval(item.FR_Rule.trim());
          } catch (err) {
            data[item.FR_TableField] = undefined;
            console.error(`窗体模型${item.FR_TableField}字段规则${item.FR_Rule}异常`);
          }
        }
      } else if (item.FR_RuleType === "trigger") {
        console.log("trigger", item);
        let str = item.FR_Rule.trim();
        if (str.includes("LSH:")) {
          //获取流水号
          const Res: any = await vueAxios.post(
            "Dynamic/FunctionGetMaxByCondition",
            { tableName: tableName, fieldRule: item.FR_Rule, data: data },
            { noLoading: true }
          );
          if (Res.Mark) {
            data[item.FR_TableField] = Res.Value;
          }
          console.log(data[item.FR_TableField], item.FR_TableField);
        } else if (str.includes("constant:")) {
          let regex = /\[([^\]]+)\]/g;
          let regex2 = /\{([^{}]+)\}/g;
          let result = str.match(regex); //获取所有使用[]包含的内容
          let result2 = str.match(regex2); //获取所有使用{}包含的内容
          for (let i = 0; i < result.length; i++) {
            const item2 = result[i];
            str = str.replace(item2, this.catalogStore.treeNode.Catalog_Code);
          }
          for (let i = 0; i < result2.length; i++) {
            const item2 = result2[i];
            const newItem = removeBlock(item2).split(":")[1];
            if (item2.includes("padLeft")) {
              const length = removeBlock(item2).split(":")[2];
              const num = this.PadLeft(data[newItem], parseInt(length));
              str = str.replace(item2, num);
            }
            str = str.replace(item2, data[newItem]);
          }
          data[item.FR_TableField] = str;
          console.log(data[item.FR_TableField]);
        } else if (str.includes("arithmetic:")) {
          let regex = /\{([^{}]+)\}/g;
          let result = str.match(regex); //获取所有使用{}包含的内容
          result.forEach((item2: any) => {
            const newItem = removeBlock(item2);
            if (item2.includes("+") || item2.includes("-") || item2.includes("*") || item2.includes("/")) {
              const pattern = /[+\-*/]/g; // 使用正则表达式模式进行匹配
              const newItemArr = newItem.split(pattern);
              newItemArr.forEach((item3: any) => {
                str = str.replace(item3, data[item3]);
                item2 = item2.replace(item3, data[item3]);
              });
              str = str.replace(item2, eval(item2));
            }
            str = str.replace(item2, data[newItem]);
          });
          data[item.FR_TableField] = eval(str) ? eval(str) : "";
        } else if (str.includes("intercept:")) {
          const newStr = removeBlock(str);
          str = data[newStr.split(":")[1]] ? data[newStr.split(":")[1]].slice(newStr.split(":")[2]) : "";
          data[item.FR_TableField] = str;
        }
      }
    }
  }
  /**
   * @名字: 不足百位,前面补零
   * @详情:仅适用于对number的判断
   * @参数:val代表传进来的数字,length代表数据长度,默认为2
   * @返回值:
   */
  PadLeft(val: string, length = 3) {
    return (Array(length).join("0") + val).slice(-length);
  }
}
